package com.htg.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;

import androidx.core.content.ContextCompat;


public class DialogWaitting {

    private Context cxt;

    private AlertDialog alertDialog = null;
    private int colorPrimary;
    private float cornerRadius;


    public DialogWaitting(Context cxt) {
        this.cxt = cxt;
        this.colorPrimary = cxt.getColor(R.color.blue_0166FF);
        cornerRadius = cxt.getResources().getDimensionPixelSize(R.dimen.dp_5);
    }

    public void show() {
        if (alertDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(cxt);
            alertDialog = builder.create();
            alertDialog.setCancelable(false);
        } else {
            if (alertDialog.isShowing()) {
                return;
            }
        }


        View view = LayoutInflater.from(cxt).inflate(R.layout.dialog_waiting, null);



        ProgressBar progressBar = view.findViewById(R.id.pb_dialog_waitting);


        alertDialog.show();

        progressBar.getIndeterminateDrawable().setColorFilter(colorPrimary, PorterDuff.Mode.SRC_IN);
        //progressBar.setProgressTintList(ColorStateList.valueOf(ContextCompat.getColor(cxt, R.color.black_FF333333)));
        //progressBar.setSecondaryProgressTintList(ColorStateList.valueOf(ContextCompat.getColor(cxt, R.color.green_3bb9ad)));

        //hehe(progressBar);
        //haha(progressBar);

        Window window = alertDialog.getWindow();

        int width = cxt.getResources().getDimensionPixelSize(R.dimen.dp_110);
        int height = cxt.getResources().getDimensionPixelSize(R.dimen.dp_110);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = width;
        layoutParams.height = height;
        window.setAttributes(layoutParams);
        // 创建一个 GradientDrawable 对象
        GradientDrawable drawableRoot = new GradientDrawable();
        drawableRoot.setShape(GradientDrawable.RECTANGLE);
        drawableRoot.setColor(cxt.getResources().getColor(R.color.white, cxt.getTheme())); // 设置背景颜色
        drawableRoot.setCornerRadius(cornerRadius);// 设置圆角半径
        window.setBackgroundDrawable(drawableRoot);
        window.setContentView(view);


    }

    private void haha(ProgressBar progressBar){
        // 设置主进度条的颜色
        progressBar.setProgressTintList(ColorStateList.valueOf(ContextCompat.getColor(cxt, R.color.blue_0166FF)));

        // 设置次进度条的颜色
        progressBar.setSecondaryProgressTintList(ColorStateList.valueOf(ContextCompat.getColor(cxt, R.color.purple_200)));

        // 设置背景进度条的颜色
        progressBar.setProgressBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(cxt, R.color.black)));
    }

    private void hehe(ProgressBar progressBar){
        // 获取 LayerDrawable 并修改颜色
        LayerDrawable layerDrawable = (LayerDrawable) progressBar.getProgressDrawable();
        Drawable progressDrawable = layerDrawable.findDrawableByLayerId(android.R.id.progress);
        progressDrawable.setColorFilter(ContextCompat.getColor(cxt, R.color.black_FF333333), PorterDuff.Mode.SRC_IN);
        // 设置次进度条颜色
        Drawable secondaryProgressDrawable = layerDrawable.findDrawableByLayerId(android.R.id.secondaryProgress);
        secondaryProgressDrawable.setColorFilter(ContextCompat.getColor(cxt, R.color.purple_200), PorterDuff.Mode.SRC_IN);
        // 设置背景颜色
        Drawable backgroundDrawable = layerDrawable.findDrawableByLayerId(android.R.id.background);
        backgroundDrawable.setColorFilter(ContextCompat.getColor(cxt, R.color.blue_0166FF), PorterDuff.Mode.SRC_IN);
    }


    public void dismis() {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
        alertDialog = null;
    }

    public boolean isShowIng(){
        if (alertDialog==null)
            return false;
        return alertDialog.isShowing();
    }


    public void setColorPrimary(int colorPrimary) {
        this.colorPrimary = colorPrimary;
    }

    public void setCornerRadius(float cornerRadius) {
        this.cornerRadius = cornerRadius;
    }
}
